package shore.android;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class FirstAndroidProjectActivity extends Activity 
{
	//operation selected from spinner
	private char selectOpr;
	
	//Listener Class for Operator Spinner
	public class MyOnItemSelectedListener implements OnItemSelectedListener 
	{
		public void onItemSelected(AdapterView<?> parent,
		View view, int pos, long id) 
		{
			//Sets the currently selected operator
			selectOpr = parent.getItemAtPosition(pos).toString().charAt(0);
		}
		public void onNothingSelected(AdapterView<?> parent) 
		{
			// Do nothing.
		}
	}//public class MyOnItemSelectedListener
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //registers button and defines its click event
        final Button button = (Button) findViewById(R.id.button_compute);         
        button.setOnClickListener(new View.OnClickListener() 
        {             
        	public void onClick(View v) 
        	{                 
        		button_compute_clicked();            
        	}         
        });
        
        //Registers and initializes spinner that contains sample operations
        Spinner spinner = (Spinner) findViewById(R.id.spinner_operators);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
        		this, R.array.OPERATORS, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		//Defines listener event for the spinner
		spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
        
		//sets the selected operator to the default
		selectOpr = '+';

    }//public void onCreate
    
    @Override
    /**
     * Called when a menu is created.
     */
    public boolean onCreateOptionsMenu(Menu menu) 
    {    
    	MenuInflater inflater = getMenuInflater();    
    	inflater.inflate(R.menu.menu, menu);    
    	return true;
    }//public boolean onCreateOptionsMenu
    
    @Override
    /**
     * Called when user selects menu item
     */
    public boolean onOptionsItemSelected(MenuItem item) 
    {    
    	// Handle item selection    
    	switch (item.getItemId()) 
    	{    
    		//starts new activity
    		case R.id.new_activity:        
    			Intent intent = new Intent(this, NewActivity.class);
    			startActivity(intent);
    			return true;  
    			
    		case R.id.help:        
    			        
    			return true; 
    			
    		default:        
    			return super.onOptionsItemSelected(item);    
    	}
    }//public boolean onOptionsItemSelected
    
    /**
     * Called when compute button clicked. Calculates result of selected operation.
     * 
     */
    public void button_compute_clicked()
    {
    	final EditText argOne = (EditText) findViewById(R.id.edit_arg_one);
    	final EditText argTwo = (EditText) findViewById(R.id.edit_arg_two);
    	final TextView sumEdit = (TextView) findViewById(R.id.text_sum);
    	double result = 0;
    	
    	try
    	{
    		//Performs the appropriate calculation based on the selected operator
    		switch(selectOpr)
    		{
	    		case '+':
	    			result = Double.parseDouble(argOne.getText().toString()) + 
						Double.parseDouble(argTwo.getText().toString());
	    			break;
	    		case '-':
	    			result = Double.parseDouble(argOne.getText().toString()) - 
						Double.parseDouble(argTwo.getText().toString());
	    			break;
	    		case '*':
	    			result = Double.parseDouble(argOne.getText().toString()) * 
						Double.parseDouble(argTwo.getText().toString());
	    			break;
	    		case '/':
	    			result = Double.parseDouble(argOne.getText().toString()) / 
						Double.parseDouble(argTwo.getText().toString());
	    			break;
    		}
    		
    		//sets result of operation
    		sumEdit.setText("" + result);
    	}
    	catch(Exception e)
    	{
    		
    	}
    	
    	
    }//public void button_compute
    
}//public class FirstAndroidProjectActivity